Skip to content

Use two decimals for delayed scale factor #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

nikeshnazareth
Copy link
Collaborator

This PR make two changes.

Firstly, move the percentage calculations to a separate library. This is cleaner in code and it also makes it easier to use different precision amounts.

The second is to use two decimals of precision for the delayed fee percentage. As a uint16 there are 65536 possible values but in basis points we can only represent numbers between 0 and 6.5 (albeit with very high precision). With two decimals we can represent numbers up to 655 (with less precision).

This is cleaner in code and it also makes it possible to provide a
configurable percentage
This rebalances the bits to be more useful for anticipated values.
A uint16 proves 65536 possible possibilities, but using basis points
means that the values range from 0-6 with 4 decimals.

As a percentage, this can represent up to 655 with two decimals.
Copy link
Collaborator

@ggonzalez94 ggonzalez94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Just a couple of comments

/// @param value The number to scale
/// @param percentage The percentage expressed in basis points
/// @return _ The scaled value
function scaleBy(uint256 value, uint16 percentage) internal pure returns (uint96) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hesitant to name the two functions the same. it might be easy to get confused by anyone using the library.
What do you think about this approach?

  • scaleBy remains the common function that takes a precision
  • scaleByBps is the bps implementation
  • scaleByPercentage is the percentage implementation

It is more verbose, but I believe it is less error prone

Comment on lines 980 to 988
function _calculatePercentageBPS(uint256 amount, uint16 percentage) internal pure returns (uint256) {
return amount * percentage / 10_000;
}

function _calculatePercentage(uint256 amount, uint16 percentage) internal pure returns (uint256) {
return amount * percentage / 100;
}


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the tests just use the library? I don't think we are trying to check if the library works properly on these tests(we can create a new unit test for LibPercentage if we wanted to do this)

It now uses scaleByBPS and scaleByPercentage explicitly
Copy link

github-actions bot commented May 19, 2025

Changes to gas cost

Generated at commit: 2796a931dca66752e22deaa6a1e161b5249ebf01, compared to commit: 0ee6812153aa4400edad00181a5f21be7aac7205

🧾 Summary (10% most significant diffs)

Contract Method Avg (+/-) %
BlobRefRegistry getRef -976 ✅ -41.36%
ERC20ProverManagerMock getCurrentFees +67 ❌ +0.96%
ETHProverManagerMock getCurrentFees +67 ❌ +0.96%

Full diff report 👇
Contract Deployment Cost (+/-) Method Min (+/-) % Avg (+/-) % Median (+/-) % Max (+/-) % # Calls (+/-)
BlobRefRegistry 468,450 (0) getRef 1,381 (-723) -34.36% 1,384 (-976) -41.36% 1,384 (-976) -41.36% 1,387 (-1,230) -47.00% 2 (0)
ERC20ProverManagerMock 0 (0) bid
evictProver
finalizePastPeriod
getCurrentFees
payPublicationFee
prove
51,939 (+147)
83,689 (+67)
57,303 (0)
6,038 (+67)
32,024 (0)
84,105 (0)
+0.28%
+0.08%
0.00%
+1.12%
0.00%
0.00%
77,049 (+117)
83,689 (+67)
57,628 (+34)
7,053 (+67)
44,136 (+1)
87,584 (+21)
+0.15%
+0.08%
+0.06%
+0.96%
+0.00%
+0.02%
84,521 (+147)
83,689 (+67)
57,628 (+34)
7,053 (+67)
46,824 (0)
88,103 (0)
+0.17%
+0.08%
+0.06%
+0.96%
0.00%
0.00%
84,521 (+147)
83,689 (+67)
57,953 (+67)
8,068 (+67)
55,374 (0)
90,519 (0)
+0.17%
+0.08%
+0.12%
+0.84%
0.00%
0.00%
5 (0)
1 (0)
2 (0)
2 (0)
40 (0)
7 (0)
ETHProverManagerMock 3,185,904 (+17,260) bid
evictProver
finalizePastPeriod
getCurrentFees
payPublicationFee
prove
51,939 (+147)
83,689 (+67)
57,303 (0)
6,038 (+67)
32,024 (0)
84,083 (0)
+0.28%
+0.08%
0.00%
+1.12%
0.00%
0.00%
77,049 (+117)
83,689 (+67)
57,628 (+34)
7,053 (+67)
44,136 (+1)
87,562 (+21)
+0.15%
+0.08%
+0.06%
+0.96%
+0.00%
+0.02%
84,521 (+147)
83,689 (+67)
57,628 (+34)
7,053 (+67)
46,824 (0)
88,081 (0)
+0.17%
+0.08%
+0.06%
+0.96%
0.00%
0.00%
84,521 (+147)
83,689 (+67)
57,953 (+67)
8,068 (+67)
55,374 (0)
90,497 (0)
+0.17%
+0.08%
+0.12%
+0.84%
0.00%
0.00%
5 (0)
1 (0)
2 (0)
2 (0)
40 (0)
7 (0)

Copy link
Collaborator

@ggonzalez94 ggonzalez94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@ggonzalez94
Copy link
Collaborator

Merging this now that it has 2 approves. I had to change the foundry version that is used in the CI to stable because nightly was causing a couple tests that use blobhash to fail. This is likely more an issue on Foundry's side than ours, but still good to keep an eye

@ggonzalez94 ggonzalez94 merged commit 3ae6136 into price-delayed-publications May 20, 2025
5 checks passed
@ggonzalez94 ggonzalez94 deleted the delayed-publications/configurable-precision branch May 20, 2025 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants